package com.xtkj.superclub_activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xtkj.superclub_activity.client.IPhotoClient;
import com.xtkj.superclub_activity.client.ITeamClient;
import com.xtkj.superclub_activity.dao.*;
import com.xtkj.superclub_activity.service.IActivityService;
import com.xtkj.superclub_entity.entity.activity.*;
import com.xtkj.superclub_entity.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service
public class ActivityServiceImpl implements IActivityService {

    @Autowired
    private IActivityCommentDao activityCommentDao;

    @Autowired
    private IActivityCommentReplyDao activityCommentReplyDao;

    @Autowired
    private IActivityDao activityDao;

    @Autowired
    private IActivityAlbumDao activityAlbumDao;

    @Autowired
    private IActivityPhotoDao activityPhotoDao;

    @Autowired
    private IActivityPraiseDao activityPraiseDao;

    @Autowired
    private IActivityTypeDao activityTypeDao;

    @Autowired
    private IActivityUserDao activityUserDao;

    @Autowired
    private IUserDao userDao;

    @Autowired
    private IPhotoClient photoClient;

    @Autowired
    private ITeamClient teamClient;

    //查询热门活动列表
    @Override
    public List<Activity> getHotActivityList() {

        QueryWrapper<Activity> wrapper = new QueryWrapper<>();

        wrapper.eq("ishot",1).gt("end_time",new Date()).orderByDesc("joined_num*0.5+isfree*0.3+read_num*0.2");

        List<Activity> activities = activityDao.selectList(wrapper);

        System.out.println(activities.toString());

        return activities;
    }

    @Override
    //查询同城活动列表
    public List<Activity> getCityActivityList(String location) {

        QueryWrapper<Activity> wrapper = new QueryWrapper<>();

        wrapper.like("location",location);

        List<Activity> activities = activityDao.selectList(wrapper);

        System.out.println(activities.toString());

        return activities;
    }

    //最新活动列表
    @Override
    public List<Activity> getNewsActiviyList() {

        QueryWrapper<Activity> wrapper = new QueryWrapper<>();

        wrapper.gt("start_time",new Date());

        List<Activity> activities = activityDao.selectList(wrapper);

        System.out.println(activities.toString());

        return activities;
    }

    //活动详情信息
    /*
    * 1、获取活动表的信息
    * 2、获取活动照片表的信息
    * 3、获取以报名人数的信息
    * 4、获取活动点赞表的信息
    * 5、获取活动评论表的信息
    * 6、获取评论回复表的信息
    * 7、获取活动类型表的信息
    * 8、更新活动表的浏览次数 +1
    * */
    @Override
    public Map<String,Object> getActivtiyInfo(String activityId) {

        Map<String,Object> activtiyInfo = new HashMap<String,Object>();

        //获取活动表的信息
        System.out.println("获取活动表的信息=====>");

        QueryWrapper wrapper1 = new QueryWrapper<>().eq("activity_id",activityId);

        Activity activity = activityDao.selectOne(wrapper1);

        activtiyInfo.put("activity",activity);

        //获取活动照片表的信息
        System.out.println("获取活动照片表的信息=====>");

        QueryWrapper wrapper2 = new QueryWrapper<>().eq("activity_id",activityId);

        List<ActivityAlbum> activityAlbums = activityAlbumDao.selectList(wrapper2);
        //拿到所有的照片信息
        List<String> activityPhotoIds = new ArrayList<String>();
        for (int i = 0;i<activityAlbums.size();i++){
            activityPhotoIds.add(i,activityAlbums.get(i).getActivityPhotoId());
        }

        QueryWrapper wrapper2_1 = new QueryWrapper<>().in("activity_photo_id",activityPhotoIds);
        List<ActivityPhoto> activityPhotos = activityPhotoDao.selectList(wrapper2_1);

        activtiyInfo.put("activityPhotos",activityPhotos);

        //获取以报名人数的信息
        System.out.println("获取以报名人数的信息=====>");

        QueryWrapper wrapper3 = new QueryWrapper<>().select("user_id").eq("activity_id",activityId);

        List<ActivityUser> activityUsers = activityUserDao.selectList(wrapper3);

        List<String> userIds = new ArrayList<String>();

        for (int i = 0;i<activityUsers.size();i++){
            userIds.add(i,activityUsers.get(i).getUserId());
        }

        QueryWrapper wrapper3_1 = new QueryWrapper<>().in("user_id",userIds);

        List<User> users = userDao.selectList(wrapper3_1);

        activtiyInfo.put("users",users);

        //获取活动点赞表的信息
        System.out.println("获取活动点赞表的信息=====>");

        QueryWrapper wrapper4 = new QueryWrapper<>().select("user_id").eq("activity_id",activityId);

        List<ActivityPraise> activityPraises = activityPraiseDao.selectList(wrapper3);

        List<String> userIds1 = new ArrayList<String>();

        for (int i = 0;i<activityPraises.size();i++){
            userIds1.add(i,activityPraises.get(i).getUserId());
        }

        QueryWrapper wrapper4_1 = new QueryWrapper<>().in("user_id",userIds1);

        List<User> userPraise = userDao.selectList(wrapper4_1);

        System.out.println("activityPraise is "+userPraise);

        activtiyInfo.put("userPraise",userPraise);

        //获取活动评论表的信息
        System.out.println("获取活动评论表的信息=====>");

        QueryWrapper wrapper5 = new QueryWrapper<>().eq("activity_id",activityId);

        List<ActivityComment> activityComments = activityCommentDao.selectList(wrapper5);

        List<Map<String,Object>> activityCommentsAndCommentUser = new ArrayList<Map<String,Object>>();

        for (int i = 0;i<activityComments.size();i++){
            QueryWrapper wrapper5_1 = new QueryWrapper<>().eq("user_id",activityComments.get(i).getUserId());

            User user = userDao.selectOne(wrapper5_1);

            Map<String,Object> commentsAndUser = new HashMap<String,Object>();

            commentsAndUser.put("activityComments",activityComments.get(i));
            commentsAndUser.put("user",user);

            activityCommentsAndCommentUser.add(i,commentsAndUser);
        }

        activtiyInfo.put("activityCommentsAndCommentUser",activityCommentsAndCommentUser);

        //获取评论回复表的信息
        System.out.println("获取评论回复表的信息=====>");

        //拿到所有的评论的id
        List<String> commentIds = new ArrayList<String>();

        for (int i = 0;i<activityComments.size();i++){
            commentIds.add(i,activityComments.get(i).getActivityCommentId());
        }

        QueryWrapper wrapper6 = new QueryWrapper<>().in("activity_comment_id",commentIds);

        List<ActivityCommentReply> activityCommentReplies = activityCommentReplyDao.selectList(wrapper6);

        List<Map<String,Object>> activityCommentRepliesAndCommentReplyUser = new ArrayList<Map<String,Object>>();

        for (int i = 0;i<activityCommentReplies.size();i++){
            QueryWrapper wrapper6_1 = new QueryWrapper<>().eq("user_id",activityCommentReplies.get(i).getUserId());

            User user = userDao.selectOne(wrapper6_1);

            Map<String,Object> commentRepliesAndUser = new HashMap<String, Object>();

            commentRepliesAndUser.put("activityCommentReply",activityCommentReplies.get(i));
            commentRepliesAndUser.put("user",user);

            activityCommentRepliesAndCommentReplyUser.add(i,commentRepliesAndUser);
        }

        activtiyInfo.put("activityCommentRepliesAndCommentReplyUser",activityCommentRepliesAndCommentReplyUser);

        //获取活动类型表的信息
        System.out.println("获取活动类型表的信息=====>");

        QueryWrapper wrapper7 = new QueryWrapper<>().eq("activity_type_id",activity.getActivityTypeId());

        ActivityType activityType = activityTypeDao.selectOne(wrapper7);

        activtiyInfo.put("activityType",activityType);

        //更新活动表的浏览次数 +1
        System.out.println("更新活动表的浏览次数 +1 =====>");

        UpdateWrapper wrapper = new UpdateWrapper<>().set("read_num",activity.getReadNum()+1).eq("activity_id",activity.getActivityId());

        activityDao.update(activity,wrapper);

        return activtiyInfo;
    }

    @Override
    @Transactional
    //发布活动
    public void sendActivity(Activity activity) {
        if(activity.getStartTime().getHours()>21 && activity.getStartTime().getHours()<24){
            //开始时间21-24
            activity.getStartTime().setTime(activity.getStartTime().getTime()+11*60*60*1000);
            //设置热门
            activity.setIshot(1);
        }else if (activity.getStartTime().getHours()>0 && activity.getStartTime().getHours()<3){
            //开始时间0-3
            activity.getStartTime().setTime(activity.getStartTime().getTime()+8*60*60*1000);
            //设置热门
            activity.setIshot(1);
        }else if (activity.getStartTime().getHours()>3 && activity.getStartTime().getHours()<6){
            //开始时间3-6
            activity.getStartTime().setTime(activity.getStartTime().getTime()+5*60*60*1000);
            //设置热门
            activity.setIshot(1);
        }
        //插入数据。。
        activityDao.insert(activity);

        String teamAlbumId = teamClient.getTeam(activity.getTeamId()).getData()+"";

        System.out.println("teamAlbumId is "+teamAlbumId);

        Map<String,Object> albumInfo = new HashMap<String, Object>();
        albumInfo.put("Type","activity");
        albumInfo.put("activityId",activity.getActivityId());
        albumInfo.put("teamAlbumId",teamAlbumId);
        albumInfo.put("albumName",activity.getTitle());

        photoClient.newAlBumList(albumInfo);

    }

    //报名活动
    @Override
    @Transactional
    public void attendActivtiy(String activityId, String userId) {

        QueryWrapper wrapper = new QueryWrapper<>().eq("activity_id",activityId);

        //查询要参加的活动
        Activity activity = activityDao.selectOne(wrapper);

        //检查活动是否已过时间，是否满员
        if(activity.getStartTime().getTime()>new Date().getTime()&&activity.getRegistrationLimit()>activity.getJoinedNum()){
            //在活动用户表中插入数据
            activityUserDao.insert(new ActivityUser(activityId,userId,new Date(),new Date()));

            //活动表中的报名人数+1
            activity.setJoinedNum(activity.getJoinedNum()+1);
            UpdateWrapper wrapper1 = new UpdateWrapper<>().eq("activity_id",activityId);
            activityDao.update(activity,wrapper1);
        }


    }


}
